var express = require('express');
var router = express.Router();
// 导入MySQL模块
var mysql = require('mysql');
var dbConfig = require('../db/config');
var userSQL = require('../db/picture');
// 使用DBConfig.js的配置信息创建一个MySQL连接池
var connection = mysql.createConnection(dbConfig.mysql); 
    connection.connect();
//为datatime提供字符串
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "/";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
}
// 添加爬虫最后一次爬取的信息
var saveLastSpider = function(link,sort,type){
    console.log('saveLastSpider')
    console.log(link)
    var timestamp=getNowFormatDate();
    var values =   [
                    [link,timestamp,parseInt(sort),parseInt(type)]
                   ];
    connection.query(userSQL.reSpiderSQL.insert,[values],function(err, result) {
            if(result) {      
                 result = {   
                          code: 200,   
                          msg:'增加成功'
                 };  
            }     
           });
}
//查询出最后一次爬取的信息,返回一个新地址
var getInfoByType = function(type,callback){
    var values =   [
                    [parseInt(type)]
                   ];
    connection.query(userSQL.reSpiderSQL.getInfoByType,[values],function(err, result) {
            if(result) {      
                callback(result[0].rs_link)
            }     
           });
}
//调用函数
getInfoByType(11,function(result){
        res.json({newUrl:result});
});
saveLastSpider('2222',11,11);